﻿using System.Collections.Generic;
using GameFramework.skill;
using UnityEngine;

namespace GameFramework.buff
{
    public class BaseBuff:IBuff
    {
        //TODO 静态数据(配置数据)，动态数据(运行时数据)
        public int id;

        public BuffConfig buffConfig;
        //TODO 再提一个子类？属性变化buff
        public Dictionary<int, float> addBeforeProperty = new Dictionary<int, float>();
        //测试用的速度
        public float speed =0;

        //buff关联的timer
        public int timerId;
        
        //buff上次执行时间(运行时数据)
        public long lastExcuteTime;
        //buff的释放者(运行时数据)
        public GameObject releaser;

        //当前buff被谁持有，挂在到谁身上的(运行时数据)
        public GameObject holder;
        //有哪个技能产生(运行时数据)
        public Skill skill;

        //buff层数(运行时数据)
        public int layer;
        
        //从添加buff开始过去的时间
        public float gone;
        //当前生效的次数
        public int times = 1;
        //实际的持续时间
        public float duration;
 
        
        //上下文，可以与传递一些其他数据(运行时数据)
        public object context;

        public void buffChanged()
        {
            //TODO 抛出buff变动的事件(增，删，刷新),ui可以监听来刷新界面的UI信息
            // EventManager.Instance.dispatch();
        }

        public virtual bool OnBuffBeforeAdd(List<BaseBuff> ownerAllBuffs)
        {
            return true;
        }

        public virtual BaseBuff OnBuffRefresh(List<BaseBuff> ownerAllBuffs)
        {
            int overlapType = buffConfig.overlapType;
            BaseBuff oldBuff = ownerAllBuffs.Find(buff =>
            {
                return buff.holder.Equals(this.holder) && (buff.id == this.id);
            });
            //TODO 抽象成刷新策略
            switch (overlapType)
            {
                case 1://层数叠加
                    if (oldBuff != null)
                    {
                        oldBuff.layer++;
                        return oldBuff;
                    }
                    return this;
                    break;
                case 2://重置时间
                    if (oldBuff != null)
                    {
                        oldBuff.duration = buffConfig.duration;
                        return oldBuff;
                    }
                    return this;
                    break;
                case 3://累加时间
                    if (oldBuff != null)
                    {
                        oldBuff.duration += buffConfig.duration;
                        return oldBuff;
                    }
                    return this;
                    break;
                default:
                    Debug.LogError("未处理的buff叠加类型："+overlapType);
                    return this;
                    break;
            }
        }

        public virtual void OnBuffAfterAdd(List<BaseBuff> ownerAllBuffs)
        {
            if (buffConfig.resetOnDestroy)
            {
                //恢复到添加前的属性？还是说恢复变化的量(因为可能同时有多个buff叠加状态)
            }
        }

        public virtual void OnIntervalTrigger(List<BaseBuff> ownerAllBuffs)
        {

        }

        public virtual void OnBuffBeforeDestroy(List<BaseBuff> ownerAllBuffs)
        {
            
        }

        public virtual void OnBuffAfterDestroy(List<BaseBuff> ownerAllBuffs)
        {
            
        }
        

        public virtual void onSkillExcute(Skill skill)
        {
           
        }

        public virtual void OnBeforeGiveDamage()
        {

        }

        public virtual void OnAfterGiveDamage()
        {

        }

        public virtual void OnBeforeTakeDamage()
        {

        }

        public virtual void OnAfterTakeDamage()
        {

        }

        public virtual void OnBeforeDead()
        {

        }

        public virtual void OnAfterDead()
        {
 
        }

        public virtual void OnKill()
        {

        }
    }
}